package com.pureut.storage.controller;


import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.storage.dto.PickingOutsourceDto;
import com.pureut.storage.export.PickingOutsourceExport;
import com.pureut.storage.service.PickingOutsourceService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.OrderReviewVo;
import com.pureut.storage.vo.PickingOutsourceVo;
import com.pureut.storage.vo.PickingRestDbVo;
import com.pureut.storage.vo.UpdateDocVo;
import com.pureut.system.api.vo.PickingOutsourceFeignVo;
import com.pureut.system.api.vo.SubcontractReceiptOtherVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 委外发货控制层
 *
 * @author: CM
 * @date: 2022/12/28  10:58
 */
@RestController
@RequestMapping("/pickingOutsource")
public class PickingOutsourceController extends BaseController {

    @Resource
    PickingOutsourceService pickingOutsourceService;

    /**
     * 新增委外发货单
     */
    @RequiresPermissions("warehouse:outbound:subcontract:add")
    @Log(title = "新增委外发货", businessType = BusinessType.INSERT)
    @PostMapping("/addOutsource")
    public AjaxResult addOutsource(@RequestBody PickingOutsourceVo pickingOutsourceVo) throws Exception {
        return toAjax(pickingOutsourceService.addOutsource(pickingOutsourceVo));
    }

    /**
     * 修改委外发货单
     */
    @RequiresPermissions("warehouse:outbound:subcontract:edit")
    @Log(title = "修改委外发货", businessType = BusinessType.UPDATE)
    @PostMapping("/updateOutsource")
    public AjaxResult updateOutsource(@RequestBody PickingOutsourceVo pickingOutsourceVo) throws Exception {
        return toAjax(pickingOutsourceService.updateOutsource(pickingOutsourceVo));
    }

    /**
     * 关结委外发货单
     */
    @RequiresPermissions("warehouse:outbound:subcontract:close")
    @Log(title = "关结委外发货", businessType = BusinessType.UPDATE)
    @PostMapping("/closeOutsource")
    public AjaxResult closeOutsource(@RequestBody PickingOutsourceVo pickingOutsourceVo) throws Exception {
        return toAjax(pickingOutsourceService.closeOutsource(pickingOutsourceVo));
    }

    /**
     * 删除单据
     */
    @RequiresPermissions("warehouse:outbound:subcontract:remove")
    @Log(title = "删除单据", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteOutsource/{ids}")
    public AjaxResult deleteOutsource(@PathVariable("ids") String ids) throws Exception {
        return toAjax(pickingOutsourceService.deleteOutsource(ids));
    }

    /**
     * 提交审核
     */
    @RequiresPermissions("warehouse:outbound:subcontract:submit")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/submitOutsource/{ids}")
    public AjaxResult submitOutsource(@PathVariable("ids") String ids) throws Exception {
        return toAjax(pickingOutsourceService.submitOutsource(ids));
    }


    /**
     * 查看单据
     */
    @RequiresPermissions("warehouse:outbound:subcontract:query")
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(pickingOutsourceService.view(id));
    }

    /**
     * 审核单据
     */
    @RequiresPermissions("warehouse:outbound:subcontract:examine")
    @Log(title = "审核单据", businessType = BusinessType.UPDATE)
    @PutMapping("/purchaseOrderReview")
    public AjaxResult purchaseOrderReview(@Validated @RequestBody OrderReviewVo orderReviewVo) {
        return toAjax(pickingOutsourceService.pickOutSourceReview(orderReviewVo));
    }

    /**
     * 查询委外出库
     */
    @GetMapping("/list")
    public TableDataInfo getList(PickingOutsourceVo pickingOutsourceVo) {
        startPage();
        //查询销售发货列表
        List<PickingOutsourceDto> list = pickingOutsourceService.getList(pickingOutsourceVo);
        return getDataTable(list);
    }

    /**
     * 导出
     */
    @RequiresPermissions("warehouse:outbound:subcontract:export")
    @PostMapping("/export")
    public AjaxResult inspectionMessageExport(HttpServletResponse response,PickingOutsourceVo pickingOutsourceVo) throws IOException {
        List<PickingOutsourceExport> infoExport = pickingOutsourceService.getInfoExport(pickingOutsourceVo);
        ExcelUtil<PickingOutsourceExport> excelUtil = new ExcelUtil<PickingOutsourceExport>(PickingOutsourceExport.class);
        excelUtil.excelWriter(response, infoExport, "委外出库信息.xlsx");
        return AjaxResult.success();
    }

    /**
     * 委外发货出库
     */
    @RequiresPermissions("warehouse:outbound:subcontract:outbound")
    @Log(title = "委外发货出库", businessType = BusinessType.UPDATE)
    @PostMapping("/out")
    public AjaxResult out(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return AjaxResult.success(pickingOutsourceService.out(pickingRestDbVo));
    }

    /**
     * 拣货完成
     */
    @GetMapping("/finishProduction/{id}")
    @Log(title = "拣货完成", businessType = BusinessType.UPDATE)
    public AjaxResult finishProduction(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(pickingOutsourceService.finishProduction(id));
    }

    /**
     * 拣货撤销
     */
    @GetMapping("/repealProduction/{id}")
    @Log(title = "拣货撤销", businessType = BusinessType.UPDATE)
    public AjaxResult repealProduction(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(pickingOutsourceService.repealProduction(id));
    }

    /**
     * 出库交接
     */
    @RequiresPermissions("warehouse:outbound:subcontract:handover")
    @Log(title = "出库交接", businessType = BusinessType.UPDATE)
    @PostMapping("/overProduction")
    public AjaxResult overProduction(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return AjaxResult.success(pickingOutsourceService.overProduction(pickingRestDbVo));
    }

    /**
     * 修改检验单状态Feign
     */
    @Log(title = "修改单据", businessType = BusinessType.UPDATE)
    @PostMapping("/outSourceUpdateStatus")
    public AjaxResult outSourceUpdateStatus(@RequestBody UpdateDocVo updateDocVo) throws Exception {
        return toAjax(pickingOutsourceService.outSourceUpdateStatus(updateDocVo));
    }

    /**
     * 拣货提示
     */
    @GetMapping("/point/{id}")
    public AjaxResult point(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(pickingOutsourceService.point(id));
    }

    /**
     * 新增（委外订单fegin调用）
     */
    @PostMapping("/addPickingOutsource")
    public AjaxResult addPickingOutsource(@RequestBody SubcontractReceiptOtherVo subcontractReceiptOtherVo) {
        return AjaxResult.success(pickingOutsourceService.addPickingOutsource(subcontractReceiptOtherVo));
    }

    /**
     * 关结委外发货单(fegin调用)
     */
    @PostMapping("/closeOutsourceFeign")
    public AjaxResult closeOutsourceFeign(@RequestBody PickingOutsourceFeignVo pickingOutsourceFeignVo) throws Exception {
        return toAjax(pickingOutsourceService.closeOutsourceFeign(pickingOutsourceFeignVo));
    }

}
